package com.edclol.apitest.tableapi;

import com.edclol.apitest.tableapi.pojo.JsonRootBean;
import com.google.gson.Gson;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011;

import java.util.Properties;


public class TableTest4_KafkaPipeLine4 {
    public static void main(String[] args) throws Exception{

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "node105:9092");
        properties.setProperty("group.id", "consumer-group6");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
//        properties.setProperty("auto.offset.reset", "latest");
        properties.setProperty("auto.offset.reset", "earliest");

        // 读取数据
        DataStream<String> inputStream = env.addSource( new FlinkKafkaConsumer011<String>("json1", new SimpleStringSchema(), properties));

        SingleOutputStreamOperator<JsonRootBean> dataStream = inputStream.map(line -> {
            Gson gson = new Gson();
            JsonRootBean bean = gson.fromJson(line, JsonRootBean.class);
            return bean;
        });


        dataStream.print();

        env.execute();
    }
}
